import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
import Qt.database.sqlite 1.0
import Control.Class 1.0


import "./mainmenu"

Rectangle {
    property string time: ""
  //  property string kkk: "value"
    property int itemCount: 0
    property var codeList: new Array
    property var titleList: new Array
    property var timeList: new Array
    function getTime()
    {
        var d = new Date();
        var years = d.getFullYear();
        var month = add_zero(d.getMonth()+1);
        var days = add_zero(d.getDate());
        var hours = add_zero(d.getHours());
        var minutes = add_zero(d.getMinutes());
        var seconds=add_zero(d.getSeconds());
        var ndate = years+"-"+month+"-"+days+"\n"+hours+":"+minutes+":"+seconds;
        return ndate;
    }

    function add_zero(temp)
    {
        if(temp<10) return "0"+temp;
        else return temp;
    }

    width: 800; /*height: 600*/
    // color: "green"
    // border.color: "blue"; border.width: 3

    //读取数据库的类
    SqliteDB{
        id: db
    }

    InstructItem{
        id: label;
        height: 15
        fontcolor: qsTr("black"); fontsize: 15
        textname: qsTr("实践日志 - 公用")
        anchors{
            left: parent.left; right: parent.right; top: parent.top;
            topMargin: 2
        }
    }

    Canvas{
        id: canvas;
        height: 10;// width: 800
        onPaint: {
            var ctx = getContext("2d")
            ctx.strokeStyle = "black"
            ctx.lineWidth = 1;
            ctx.beginPath()
            ctx.moveTo(0, 7)
            ctx.lineTo(width, 10)
            //  ctx.lineTo(220, 180)
            ctx.closePath()
            ctx.stroke()
        }
        anchors{
            top: label.bottom; left: parent.left; right: parent.right;
        }
    }

    InstructItem{
        id: hint;
        height: 15
        fontcolor: qsTr("black"); fontsize: 15
        textname: qsTr("点击一个消息便可打开。")
        anchors{
            left: parent.left; right: parent.right; top: canvas.bottom;
            topMargin: 2
        }
    }

    Rectangle{
        id: toolbar
        height: 50
        color: qsTr("#C0C0C0")
        Row{
            InstructItem{textname: qsTr("另存所有日志为..."); width: parent.width/5; backcolor: "#C0C0C0"; textVLocate: 1/*; textHLocate: 1*/}
            InstructItem{textname: qsTr("删除  ▲"); width: parent.width/5; backcolor: "#C0C0C0"; textVLocate: 1; textHLocate: 1}
            InstructItem{textname: qsTr("更新"); width: parent.width/5; backcolor: "#C0C0C0"; textVLocate: 1; textHLocate: 1}
            InstructItem{textname: qsTr(""); width: parent.width/5; backcolor: "#C0C0C0"; textVLocate: 1; textHLocate: 1}
            InstructItem{textname: qsTr("视图  ▲"); width: parent.width/5; backcolor: "#C0C0C0"; textVLocate: 1; textHLocate: 1}

            anchors.fill: parent
        }

        anchors{
            bottom: parent.bottom; left: parent.left; right: parent.right
        }
    }
    //TableView的模型，视图以及委托=================================================================================
    ListModel{
        id: listmodel;
//        Repeater{
//            model: itemCount/3;
//            ListElement{code: qsTr("10015"); title: qsTr("已选择手动模式"); time: "data-time"}
//        }

//        ListElement{code: qsTr("10015"); title: qsTr("已选择手动模式"); time: "data-time"}
    }


    Component{
        id: codedelegate
        Item{
            id: wrapper
            width: parent.widht
            height: 30
            MouseArea{
                anchors.fill: parent;
                onClicked:{
                    wrapper.ListView.view.currentIndex = index
                    mouse.accepted = true;
                }
            }

            RowLayout{
                anchors.left: parent.left; anchors.verticalCenter: parent.verticalCenter
                spacing: 8
//                Item{
//                    Image {
//                        id: image
//                        source: "../images/log0.png"
//                        width: 30; height: 30
//                        anchors.top: parent.top; anchors.left: parent.left
//                    }

                Text{
                    text: code
                    font.pixelSize: 20
                    verticalAlignment: Text.AlignVCenter
                    //       anchors.left: image.right
                    Layout.preferredWidth: tableview.width/5
                    color: wrapper.ListView.isCurrentItem ?  "lightblue" : "black"

                }

                Layout.preferredWidth: tableview.width/5
                //                }

                Text {
                    text: title
                    height: 30
                    font.pixelSize: 20
                    verticalAlignment: Text.AlignVCenter
                    Layout.preferredWidth: tableview.width*9/20
                    color: wrapper.ListView.isCurrentItem ?  "lightblue" : "black"
                }

                Text {
                    text: time
                    height: 30
                    font.pixelSize: 20
                    Layout.preferredWidth: tableview.width*7/20
                    color: wrapper.ListView.isCurrentItem ?  "lightblue" : "black"
                }
            }
        }
    }

    //头标题的委托
    Component{
        id: headerview
        Rectangle{
            color: "lightgray"
            width: parent.width; height: 30

            RowLayout{
                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                spacing: 8
                Text{
                    text: qsTr("代码")
                    font.bold: true
                    font.pixelSize: 20
                    Layout.preferredWidth: tableview.width/5
//                    Layout.preferredWidth:120
                }

                Text {
                    text: qsTr("标题")
                    font.bold: true
                    font.pixelSize: 20
                    Layout.preferredWidth: tableview.width*9/20
//                    Layout.preferredWidth:80
                }

                Text {
                    text: qsTr("日期和时间")
                    font.bold: true
                    font.pixelSize: 20
                    Layout.preferredWidth: tableview.width*7/20
//                    Layout.preferredWidth:50
                }

            }

        }
    }
    ListView{
        id: tableview; header: headerview; delegate: codedelegate
//         model: listmodel
        //model:listmodel
        model:listmodel

//        TableViewColumn{role: qsTr("code"); title: qsTr("代码"); width: tableview.width/5; delegate: codedelegate}
//        TableViewColumn{role: qsTr("title"); title: qsTr("标题"); width: tableview.width*9/20; /*delegate: coldelegate*/}
//        TableViewColumn{role: qsTr("date"); title: qsTr("日期和时间"); width: tableview.width*7/20; /*delegate: coldelegate*/}

        highlight: Rectangle{color: "lightblue"}

        function addOne(i){
            model.append({"code": codeList[i], "title": titleList[i], "time": timeList[i]})
        }

        anchors{
            top: hint.bottom; bottom: toolbar.top; left: parent.left; right: parent.right
        }

    }


    //=============================================================================================================
    onVisibleChanged: {
        canvas.requestPaint()
    }

    Component.onCompleted: {
        db.connectDB();
        db.getDBData(3, "select * from EventLog")
        db.closeDB();
        itemCount = db.strCount()/3;
        for(var i= 0; i<db.strCount(); i += 3)
        {
            codeList.push(db.getData(i))
            titleList.push(db.getData(i+1))
            timeList.push(db.getData(i+2))
        }
 //       listmodel.append({"code": "codeList[0]", "title": "titleList[0]", "time": "timeList[0]"})
        for(var i =0; i< itemCount; i++)
            tableview.addOne(i)
        console.log("代码个数："+codeList.length +"\t"+codeList[0]+"——"+codeList[itemCount-1])
        console.log("标题个数："+titleList.length +"\t"+titleList[0]+"——"+titleList[itemCount-1])
        console.log("时间个数：" + timeList.length +"\t"+timeList[0]+"——"+timeList[itemCount-1])
        //time = getTime()
    }

}
